package com.rent.reserve.controller;

import java.util.Arrays;
import java.util.List;

import com.rent.apartment.service.IApartmentInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.rent.common.annotation.Log;
import com.rent.common.enums.BusinessType;
import com.rent.reserve.domain.Reserve;
import com.rent.reserve.service.IReserveService;
import com.rent.common.core.controller.BaseController;
import com.rent.common.core.domain.AjaxResult;
import com.rent.common.utils.poi.ExcelUtil;
import com.rent.common.core.page.TableDataInfo;


/**
 * 租房订单Controller
 * 
 * @author cooper
 * @date 2024-10-15
 */
@Controller
@RequestMapping("/reserve/reserveInfo")
public class ReserveController extends BaseController
{
    private String prefix = "reserve/reserveInfo";

    @Autowired
    private IReserveService reserveService;



    @RequiresPermissions("reserve:reserveInfo:view")
    @GetMapping()
    public String reserveInfo()
    {
        return prefix + "/reserveInfo";
    }

    /**
     * 查询租房订单列表
     */
    @RequiresPermissions("reserve:reserveInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Reserve reserve)
    {
        startPage();
        List<Reserve> list = reserveService.selectReserveList(reserve);
        return getDataTable(list);
    }

    /**
     * 导出租房订单列表
     */
    @RequiresPermissions("reserve:reserveInfo:export")
    @Log(title = "租房订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Reserve reserve)
    {
        List<Reserve> list = reserveService.selectReserveList(reserve);
        ExcelUtil<Reserve> util = new ExcelUtil<Reserve>(Reserve.class);
        return util.exportExcel(list, "租房订单数据");
    }

    /**
     * 新增租房订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存租房订单
     */
    @RequiresPermissions("reserve:reserveInfo:add")
    @Log(title = "租房订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@DateTimeFormat(pattern = "yyyy-MM-dd") Reserve reserve)
    {
        return toAjax(reserveService.insertReserve(reserve));
    }

    /**
     * 修改租房订单
     */
    @RequiresPermissions("reserve:reserveInfo:edit")
    @GetMapping("/edit/{reserveId}")
    public String edit(@PathVariable("reserveId") Long reserveId, ModelMap mmap)
    {
        Reserve reserve = reserveService.selectReserveByReserveId(reserveId);
        mmap.put("reserve", reserve);
        return prefix + "/edit";
    }

    /**
     * 修改保存租房订单
     */
    @RequiresPermissions("reserve:reserveInfo:edit")
    @Log(title = "租房订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@DateTimeFormat(pattern = "yyyy-MM-dd") Reserve reserve)
    {
        return toAjax(reserveService.updateReserve(reserve));
    }

    /**
     * 删除租房订单
     */
    @RequiresPermissions("reserve:reserveInfo:remove")
    @Log(title = "租房订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        String[] idStrings = ids.split(",");
        Long[] longIds = Arrays.stream(idStrings)
                .map(idStr -> Long.parseLong(idStr.trim()))
                .toArray(Long[]::new);

        return toAjax(reserveService.deleteReserveByReserveIds(longIds));
    }

    //改用：预约
}
